package com.parking.admin.repository;

import com.parking.admin.entity.Parking;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;

/**
 * Created by hujianjun on 2015/11/9.
 */
public interface ParkingRepository extends JpaRepository<Parking, Long> {


    @Query("select p from  Parking p  where p.lng is null")
    List<Parking> findAll();

    @Query("select p from  Parking p  where p.name like :key")
    Page<Parking> findByKey(Pageable pageable, @Param("key") String key);

    @Query("update Parking p set p.lastEditor=:editor, p.lastTime=:time, p.lng=:lng, p.lat=:lat where p.id=:idParking")
    int updateLngLat1(@Param("editor") String editor, @Param("time") Date time, @Param("idParking") long idParking, @Param("lng") String lng, @Param("lat") String lat);

      @Modifying
    @Query("update Parking p set p.lng=:lng, p.lat=:lat where p.id=:idParking")
    int updateLngLat(@Param("idParking") long idParking, @Param("lng") String lng, @Param("lat") String lat);


    @Modifying
    @Query("update Parking p set p.changeLevel=:changeLevel,p.changeAddress=:changeAddress,p.changeLng=:changeLng, p.changeLat=:changeLat, p.lng=IFNULL(lng,:changeLng), p.lat=IFNULL(lat,:changeLat) where p.id=:idParking")
    int updateParkingAll(@Param("idParking") long idParking,  @Param("changeLevel") String changeLevel, @Param("changeAddress") String changeAddress,@Param("changeLng") String changeLng, @Param("changeLat") String changeLat);

}
